package com.mes.web.controller.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import com.mes.biz.domain.*;
import com.mes.biz.service.BizInventoryOrderDetailService;
import com.mes.biz.service.BizProductService;
import com.mes.biz.service.BizWarehouseService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.service.BizInventoryOrderService;

/**
 * 盘点单Controller
 * @author wy
 * @email wy
 * @date 2023-06-19
 */
@RestController
@RequestMapping("/biz/bizInventoryOrder")
public class BizInventoryOrderController extends BaseController {

    @Autowired
    private BizInventoryOrderService bizInventoryOrderService;

    @Autowired
    private BizInventoryOrderDetailService bizInventoryOrderDetailService;

    @Resource
    private BizProductService bizProductService;

    @Resource
    private BizWarehouseService bizWarehouseService;

    /**
     * 查询盘点单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOrder:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizInventoryOrder bizInventoryOrder, HttpServletRequest request, HttpServletResponse response) {
        bizInventoryOrder.setPage(new PageDomain(request, response));
        PageInfo<BizInventoryOrder> page = bizInventoryOrderService.findPage(bizInventoryOrder);
        List<BizInventoryOrder> list = page.getList();
        for (BizInventoryOrder inventoryOrder : list) {
            if(StringUtils.isNotEmpty(inventoryOrder.getWarehouseId())) {
                BizWarehouse warehouse = bizWarehouseService.get(inventoryOrder.getWarehouseId());
                if(null!=warehouse) {
                    inventoryOrder.setWarehouseName(warehouse.getWarehouseName());
                }
            }

        }

        return R.data(page);
    }

    /**
     * 获取盘点单详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOrder:query')")
    @GetMapping(value = "/{id}")
    public R<BizInventoryOrder> detail(@PathVariable("id") String id) {
        BizInventoryOrder bizInventoryOrder = bizInventoryOrderService.get(id);
        BizInventoryOrderDetail detail = new BizInventoryOrderDetail();
        detail.setInventoryOrderId(id);
        List<BizInventoryOrderDetail> list = bizInventoryOrderDetailService.findList(detail);
        for (BizInventoryOrderDetail bizInventoryOrderDetail : list) {
            BizProduct bizProduct = bizProductService.getBizProductByProductCode(bizInventoryOrderDetail.getProductCode());
            bizInventoryOrderDetail.setBrand(bizProduct.getBrand());
            bizInventoryOrderDetail.setSpec(bizProduct.getSpec());
            bizInventoryOrderDetail.setColor(bizProduct.getColor());
            List<Map<String,Object>> utilList = new ArrayList<>();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("key",1);
            map.put("name",bizProduct.getUnit());
            utilList.add(map);
            map = new HashMap<String,Object>();
            map.put("key",2);
            map.put("name",bizProduct.getAssistUnitA());
            utilList.add(map);
            map = new HashMap<String,Object>();
            map.put("key",3);
            map.put("name",bizProduct.getAssistUnitB());
            utilList.add(map);
            bizInventoryOrderDetail.setUnitList(utilList);
        }
        bizInventoryOrder.setDetailList(list);
        return R.data(bizInventoryOrder);
    }

    /**
     * 新增盘点单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOrder:add')")
    @Log(title = "盘点单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizInventoryOrder bizInventoryOrder) {
        return R.status(bizInventoryOrderService.insertBizInventoryOrder(bizInventoryOrder));
    }

    /**
     * 修改盘点单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOrder:edit')")
    @Log(title = "盘点单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizInventoryOrder bizInventoryOrder) {
        return R.status(bizInventoryOrderService.updateBizInventoryOrder(bizInventoryOrder));
    }

    /**
     * 删除盘点单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOrder:remove')")
    @Log(title = "盘点单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizInventoryOrderService.deleteBizInventoryOrderByIds(ids));
    }


    /**
     * 导出盘点单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOrder:export')")
    @Log(title = "盘点单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizInventoryOrder bizInventoryOrder) {
        List<BizInventoryOrder> list = bizInventoryOrderService.findList(bizInventoryOrder);
        ExcelUtil<BizInventoryOrder> util = new ExcelUtil<BizInventoryOrder>(BizInventoryOrder.class);
        return util.exportExcel(list, "盘点单数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    /**
     * 审核
     */
    @GetMapping(value = "/audit/{id}")
    public R auditOrder(@PathVariable String id) {
        return R.status(bizInventoryOrderService.auditBizInventoryOrder(id));
    }

    /**
     * 反审核
     */
    @GetMapping(value = "/doAudit/{id}")
    public R doAuditOrder(@PathVariable String id) {
        return R.status(bizInventoryOrderService.doAuditBizInventoryOrder(id));
    }

}
